"""Build DTX tests"""


def scons():
    """Execute build"""
    Import('denv', 'vts_objs')

    # build dtx_tests

    libraries = ['abt', 'bio', 'dtx', 'vos', 'gurt', 'daos_common_pmem', 'cmocka', 'pthread',
                 'uuid', 'cart', 'daos_tests']

    tenv = denv.Clone()
    tenv.Append(CPPPATH=[Dir('..').srcnode()])
    tenv.Append(CPPPATH=[Dir('../../vos').srcnode()])
    tenv.Append(CPPPATH=[Dir('../../vos/tests').srcnode()])
    tenv.AppendUnique(CPPPATH=[Dir('../../common/tests').srcnode()])
    tenv.require('argobots')
    tenv.AppendUnique(RPATH_FULL=['$PREFIX/lib64/daos_srv'])
    tenv.Append(OBJPREFIX="b_")

    test_src = [
        '../../common/tls.c',
        'dts_local_rdb.c',
        'dts_local.c',
        'dts_utils.c',
        'dtx_tests.c',
        'pl_map_mock.c',
        'sched_mock.c',
        'srv_mock.c',
        'srv_module_mock.c',
        'ult_mock.c',
        vts_objs
    ]
    dtx_tests = tenv.d_program('dtx_tests', test_src, LIBS=libraries)

    # build dtx_ut

    libraries = ['abt', 'bio', 'cmocka', 'daos_common_pmem', 'gurt', 'uuid', 'vea', 'pthread',
                 'pmemobj']

    tenv = denv.Clone()
    tenv.Append(CPPPATH=[Dir('../../vos').srcnode()])
    tenv.require('pmdk')
    tenv.AppendUnique(RPATH_FULL=['$PREFIX/lib64/daos_srv'])
    tenv.Append(OBJPREFIX="c_")

    # Required for vos_dtx_discard_invalid() tests.
    # These functions are validated by their respective unit tests.
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=ilog_is_valid'])
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=vos_irec_is_valid'])
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=evt_desc_is_valid'])
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=dbtree_lookup'])
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=vos_tls_get'])
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=lrua_array_aggregate'])
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=dbtree_delete'])
    tenv.AppendUnique(LINKFLAGS=['-Wl,--wrap=d_tm_dec_gauge'])

    vos_src = Glob('../../vos/*.c')

    test_src = [
        'dts_discard_invalid.c',
        'dts_structs.c',
        'dtx_ut.c',
        'sched_mock.c',
        'srv_mock.c',
        'srv_module_mock.c',
        'dts_cmt_stat.c',
        'dts_aggregate.c'
    ]
    dtx_ut = tenv.d_program('dtx_ut', test_src + vos_src, LIBS=libraries)

    # install both

    tenv.Install('$PREFIX/bin/', [dtx_tests, dtx_ut])


if __name__ == "SCons.Script":
    scons()
